/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ChenGuo
 */
import java.util.*;
import java.util.function.Function;

/**
 * 神经网络数据生成器
 * 生成训练和测试用的输入输出数据对
 */
public class DataGenerator {
    
    /**
     * 生成二维输入样本
     */
    public static List<double[]> generateInputs(int samples, double minVal, double maxVal) {
        List<double[]> inputs = new ArrayList<>();
        Random random = new Random();
        
        for (int i = 0; i < samples; i++) {
            double x = minVal + (maxVal - minVal) * random.nextDouble();
            double y = minVal + (maxVal - minVal) * random.nextDouble();
            inputs.add(new double[]{x, y});
        }
        
        return inputs;
    }
    
    /**
     * 根据目标函数生成对应输出值
     */
    public static List<Double> generateTargets(List<double[]> inputs, 
                                             Function<double[], Double> targetFunction) {
        List<Double> targets = new ArrayList<>();
        
        for (double[] input : inputs) {
            targets.add(targetFunction.apply(input));
        }
        
        return targets;
    }
    
    // ==================== 预定义目标函数 ====================
    
    /** 二次函数: z = x² + y */
    public static final Function<double[], Double> TARGET_FUNCTION_1 = 
        inputs -> inputs[0] * inputs[0] + inputs[1];
    
    /** 三角函数: z = sin(x) + cos(y) */
    public static final Function<double[], Double> TARGET_FUNCTION_2 = 
        inputs -> Math.sin(inputs[0]) + Math.cos(inputs[1]);
    
    /** 指数函数: z = x*y + e^x */
    public static final Function<double[], Double> TARGET_FUNCTION_3 = 
        inputs -> inputs[0] * inputs[1] + Math.exp(inputs[0]);
    
    /** 线性函数: z = 2x + 3y + 1 */
    public static final Function<double[], Double> TARGET_FUNCTION_4 = 
        inputs -> 2 * inputs[0] + 3 * inputs[1] + 1;
    
    /** 三次多项式: z = x³ - 2x²y + y² */
    public static final Function<double[], Double> TARGET_FUNCTION_5 = 
        inputs -> Math.pow(inputs[0], 3) - 2 * inputs[0] * inputs[0] * inputs[1] + inputs[1] * inputs[1];
    
    /** 高斯函数: z = e^(-(x²+y²)) */
    public static final Function<double[], Double> TARGET_FUNCTION_6 = 
        inputs -> Math.exp(-(inputs[0] * inputs[0] + inputs[1] * inputs[1]));
    
    /** 绝对值函数: z = |x| + |y| */
    public static final Function<double[], Double> TARGET_FUNCTION_7 = 
        inputs -> Math.abs(inputs[0]) + Math.abs(inputs[1]);
    
    /** 对数函数: z = log(1 + |x|) + log(1 + |y|) */
    public static final Function<double[], Double> TARGET_FUNCTION_8 = 
        inputs -> Math.log(1 + Math.abs(inputs[0])) + Math.log(1 + Math.abs(inputs[1]));
    
    /** 双曲正切: z = tanh(x) + tanh(y) */
    public static final Function<double[], Double> TARGET_FUNCTION_9 = 
        inputs -> Math.tanh(inputs[0]) + Math.tanh(inputs[1]);
}